package com.dinner.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dinner.auto.JSONAuthentication;
import com.dinner.entity.DiSeat;
import com.dinner.mapper.DiSeatMapper;
import com.dinner.response.Result;
import com.dinner.service.DiSeatService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dinner
 * @since 2022-02-13
 */
@RestController
@RequestMapping("/di-seat")
public class DiSeatController extends JSONAuthentication {

    @Autowired
    DiSeatMapper diSeatMapper;
    @Autowired
    DiSeatService diSeatService;
    @ApiOperation("添加座位")
    @PostMapping("/add")
    public void addSeat(HttpServletRequest request, HttpServletResponse response) throws Exception{
        DiSeat diSeat=new DiSeat();
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        diSeat.setCreatTime(timestamp);
        if(diSeatMapper.insert(diSeat)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation("删除座位")
    @PostMapping("/del")
    public void delSeat(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception{
        if(diSeatMapper.deleteById(id)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation("根据座位号id修改当前座位状态")
    @PostMapping("/updByid")
    public void updSeatByid(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("seStatus") int seStatus) throws Exception{
        DiSeat diSeat=diSeatMapper.selectById(id);
        diSeat.setSeStatus(seStatus);
        if(seStatus==0){
            diSeat.setYdId(0);
        }
        if(diSeatMapper.updateById(diSeat)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation("查询所有座位")
    @PostMapping("/look")
    public void lookSeat(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size,@RequestParam("current") int current) throws Exception{
        List<DiSeat> list=diSeatService.lookAll(current,size);
        int total=diSeatMapper.selectCount(null);
        if(list.size()>0){
            Result ok = Result.ok().data("seat",list).data("total",total);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation("根据用户id查询座位")
    @PostMapping("/lookByUserId")
    public void lookSeatByUserId(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("yd_id",userId);
        List<DiSeat> diSeat=diSeatMapper.selectList(wrapper);

        if(diSeat.size()==0){
            Result error = Result.error().message("该用户未预定餐桌");
            this.WriteJSON(request, response, error);
        }else{
            Result ok = Result.ok().data("diSeat",diSeat);
            this.WriteJSON(request, response, ok);
        }

        }

    @ApiOperation("查询出所有未预定的餐桌")
    @PostMapping("/lookWyd")
    public void lookSeatByUserIddas(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size,@RequestParam("current") int current) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("se_status",0);
        Page<DiSeat> page=new Page<>(current,size);
        Page<DiSeat> pages=diSeatMapper.selectPage(page,wrapper);
        List<DiSeat> list=pages.getRecords();

        if(list.size()>0){
            Result ok = Result.ok().data("diSeat",list);
            this.WriteJSON(request, response, ok);

        }else{
            Result error = Result.error().message("没有可预订的餐桌了");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation("根据未预定的餐桌预定")
    @PostMapping("/ydcz")
    public void YDCZ(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("userId") int userId) throws Exception{
      DiSeat diSeat=diSeatMapper.selectById(id);
     if(diSeat.getSeStatus()==0){
         diSeat.setSeStatus(1);
         diSeat.setYdId(userId);
         if (diSeatMapper.updateById(diSeat)>0){
             Result ok = Result.ok().message("预定成功");
             this.WriteJSON(request, response, ok);
         }else{
             Result error = Result.error().message("预定失败");
             this.WriteJSON(request, response, error);
         }
     }
    }
}

